package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.AppletMember;
import com.ruoyi.fly.domain.Notice;
import com.ruoyi.fly.domain.vo.NoticeInsertVo;
import com.ruoyi.fly.domain.vo.NoticeSearchVo;
import com.ruoyi.fly.domain.vo.NoticeUpdateVo;
import com.ruoyi.fly.domain.vo.NoticeVo;
import com.ruoyi.fly.enums.NoticeType;
import com.ruoyi.fly.mapper.AppletMemberMapper;
import com.ruoyi.fly.mapper.NoticeMapper;
import com.ruoyi.fly.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private AppletMemberMapper appletMemberMapper;

    /**
     * 根据id查询公告
     *
     * @param id
     * @return
     */
    @Override
    public Notice selectNoticeById(String id) {
        return noticeMapper.selectNoticeById(id);
    }

    /**
     * 查询公告列表
     *
     * @param noticeSearchVo
     * @return
     */
    @Override
    public List<NoticeVo> selectNoticeList(NoticeSearchVo noticeSearchVo) {
        List<NoticeVo> notices = noticeMapper.selectNoticeList(noticeSearchVo);
        notices.forEach(notice -> {
            if (appletMemberMapper.selectAppletMemberById(notice.getMid()) != null){
                notice.setUserName(appletMemberMapper.selectAppletMemberById(notice.getMid()).getNickname());
            }else {
                notice.setUserName("未知");
            }
            notice.setNoticeTypeName(NoticeType.getValue(Integer.valueOf(notice.getNoticeType())));
        });
        return notices;
    }

    /**
     * 新增公告
     *
     * @param notice
     * @return
     */
    @Override
    public R insertNotice(NoticeInsertVo notice) {
        notice.setCreateUser(SecurityUtils.getUserId());
        return noticeMapper.insertNotice(notice) > 0 ? R.ok("新增成功") : R.fail("操作失败");
    }

    /**
     * 修改公告
     *
     * @param notice
     * @return
     */
    @Override
    public R updateNotice(NoticeUpdateVo notice) {
        notice.setUpdateUser(SecurityUtils.getUserId());
        return noticeMapper.updateNotice(notice) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    @Override
    public R deleteNoticeById(String id) {
        return noticeMapper.deleteNoticeById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteNoticeByIds(String[] ids) {
        return noticeMapper.deleteNoticeByIds(ids) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }
}
